-- AdmDegree
set identity_insert AdmDegree on
INSERT INTO AdmDegree (Id, Name, Description)
SELECT DD.Id, DD.Name, DD.Description FROM PHSSQL195.CDS.dbo.DmnDegree DD 
set identity_insert AdmDegree off


-- AdmFacility
set identity_insert AdmFacility on
INSERT INTO AdmFacility (Id, Name, AdmFacilityTypeId, ParentFacilityId, PSCode)
SELECT DF.Id, DF.Name, DF.FacilityTypeId, DF.ParentFacilityId, DF.PSCode
FROM PHSSQL195.CDS.dbo.DmnFacility DF
set identity_insert AdmFacility off

-- FwkDomainPrincipal
set identity_insert FwkDomainPrincipal on
INSERT INTO FwkDomainPrincipal (Id, FwkDomainPrincipalTypeId, IsActive)
SELECT DP.Id, DP.DmnPrincipalTypeId as 'FwkDomainPrincipalTypeId', DP.IsActive
FROM PHSSQL195.CDS.dbo.DmnPrincipal DP
set identity_insert FwkDomainPrincipal off

-- FwkDomainUser
set identity_insert FwkDomainUser on
INSERT INTO FwkDomainUser (Id, Username, FwkApplicationBitmask, FwkPrincipalId)
SELECT DU.Id, DU.Username, DU.DmnApplicationBitmask AS 'FwkApplicationBitmask', 
DU.DmnPrincipalId AS 'FwkPrincipalId'
FROM PHSSQL195.CDS.dbo.DmnUser DU 
set identity_insert FwkDomainUser off

-- AdmPerson
set identity_insert AdmPerson on
INSERT INTO AdmPerson (Id, FirstName, LastName, Prefix, Suffix, MiddleName, 
Country, Gender, BirthDate, FwkDomainUserId, AdmPersonTypeId, MergeToId)
SELECT DP.Id, DP.FirstName, DP.LastName, DP.Prefix, DP.Suffix, DP.MiddleName, 
DP.Country, DP.Gender, DP.BirthDate, DU.Id as 'FwkDomainUserId', 
1 as 'AdmPersonTypeId', null as 'MergeToId'
FROM PHSSQL195.CDS.dbo.DmnPerson DP
INNER JOIN PHSSQL195.CDS.dbo.DmnUser DU
ON DP.Id = DU.DmnPersonId
set identity_insert AdmPerson off

-- AdmTelephoneNumber 
set identity_insert AdmTelephoneNumber on
INSERT INTO AdmTelephoneNumber (Id, CountryCode, AreaCode, Number, AdmPersonId, AdmTelephoneTypeId)
SELECT DT.Id, DT.CountryCode, DT.AreaCode, DT.Number, 
DT.DmnPersonId as 'AdmPersonId', DT.DmnTelephoneTypeId as 'AdmTelephoneTypeId'
FROM PHSSQL195.CDS.dbo.DmnTelephoneNumber DT
set identity_insert AdmTelephoneNumber off


-- AdmEmailAddress  
set identity_insert AdmEmailAddress on
INSERT INTO AdmEmailAddress (Id, EmailAddress, AdmPersonId, AdmEmailAddressTypeId, Description)
SELECT DE.Id, DE.EmailAddress, DE.DmnPersonId AS 'AdmPersonId', 
DE.DmnEmailAddressTypeId AS 'AdmEmailAddressTypeId', DE.Description
FROM PHSSQL195.CDS.dbo.DmnEmailAddress DE
set identity_insert AdmEmailAddress off

-- FwkDomainOrganization 
/*set identity_insert FwkDomainOrganization on
INSERT INTO FwkDomainOrganization (Id, Name, Description, ParentOrganizationId, ChiefCode,
BusinessUnitNumber, FullyQualifiedName, FwkDomainOrganizationTypeId, IsActive)
SELECT DO.Id, DO.Name, DO.Description, DO.ParentOrganizationId, DO.ChiefCode,
DO.BusinessUnitNumber, DO.FullyQualifiedName, 
DO.DmnOrganizationTypeId AS 'FwkDomainOrganizationTypeId', DO.IsActive
FROM PHSSQL195.CDS.dbo.DmnOrganization DO
set identity_insert FwkDomainOrganization off*/

-- FwkDomainPrincipal_Organization_Map
set identity_insert FwkDomainPrincipal_Organization_Map on
INSERT INTO FwkDomainPrincipal_Organization_Map (Id, FwkDomainPrincipalId, FwkDomainOrganizationId, 
FwkDomainOrganizationRelationshipTypeId)
SELECT DPO.Id, DPO.DmnPrincipalId AS 'FwkDomainPrincipalId',
DPO.DmnOrganizationId AS 'FwkDomainOrganizationId', 
1 AS 'FwkDomainOrganizationRelationshipTypeId'
FROM PHSSQL195.CDS.dbo.DmnPrincipal_Organization_Map DPO
inner join FwkDomainOrganization FWK
on FWK.Id = DPO.DmnOrganizationId
set identity_insert FwkDomainPrincipal_Organization_Map off

-- AdmInternalPerson
set identity_insert AdmInternalPerson on
INSERT INTO AdmInternalPerson (Id, AdmPersonId, EmployeeNumber, 
PrimaryFacilityId, PrimaryOrganizationId, StartDate, EndDate)
SELECT DP.Id, DP.Id AS 'AdmPersonId', DP.UnitNumber as 'EmployeeNumber', 
DP.PrimaryFacilityId, DP.PrimaryOrganizationId, DP.StartDate, DP.EndDate 
FROM PHSSQL195.CDS.dbo.DmnPerson DP 
INNER JOIN PHSSQL195.CDS.dbo.DmnUser DU
ON DP.Id = DU.DmnPersonId
INNER JOIN FwkDomainOrganization FWK
ON FWK.Id = DP.PrimaryOrganizationId
set identity_insert AdmInternalPerson off

-- AdmPerson_RecomAbstract_Map
INSERT INTO AdmPerson_RecomAbstract_Map (Id, AdmPersonId, RecomAbstractId, RecomUserName ) 
SELECT DT.nMapId, DT.nDmnPersonId, DT.nRECOMAbstractID, DT.sUserName 
FROM PHSSQL195.CDS.dbo.DmnUser_tblRecomAbstract_Map DT
INNER JOIN AdmPerson AP
ON AP.Id = DT.nDmnPersonId



-- CmnDate_Dim
INSERT INTO CmnDate_Dim
SELECT * FROM Insight_FeedData.dbo.CmnDate_Dim